package cn.sys.filter;

import cn.sys.entity.ManagerInfo;
import cn.sys.util.ResponseUtil;
import cn.sys.util.ResultData;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter("/managerInfo")
public class ManagerLoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String action = request.getParameter("action");
        if(action.contains("login")){
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        HttpSession session = ((HttpServletRequest) servletRequest).getSession();
        ManagerInfo managerInfo = (ManagerInfo) session.getAttribute("managerInfo");
        if(managerInfo == null){
            System.out.println("未登录");
            ResponseUtil.send((HttpServletResponse) servletResponse, ResultData.fail(ResultData.LOGIN_FAIL, "您还未登录"));
            return;
        }
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
